<?php
/**
 * 《收入管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class In extends FinanceUtil {
	public $tbl = Constant::tbl_cw_in;
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待收款',
		'2' => '正在收款',
		'3' => '完成收款',
		// '4' => '完成对帐',
		// '4' => '取消',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'customer' => '客户',
		'mold' => '产品',
		'name' => '事由',
		'date' => '日期',
		'emp' => '业务员',
		'amount' => '金额',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		
		'office' => '办公区',
		'customer' => '客户',
		'mold' => '产品',
		'currency' => '币种',
		'type' => '类型',
		'emp' => '业务员',
	
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {		
		$data = array(
			'收入信息' => array('cc'=>'finance.php?do=In.find', 'js'=>''),
		    '收入明细' => array('cc'=>'finance.php?do=InItem.find', 'js'=>''),			
			
			'支出信息' => array('cc'=>'finance.php?do=Out.find', 'js'=>''),
		    '支出明细' => array('cc'=>'finance.php?do=OutItem.find', 'js'=>''),		
			
		    '银行到帐' => array('cc'=>'finance.php?do=Transaction.find', 'js'=>''),
			'提成信息' => array('cc'=>'finance.php?do=Commission.find', 'js'=>''),
		    '客户信息' => array('cc'=>'contact.php?do=FinanceCustomer.find', 'js'=>''),		
		
			'统计分析' => array('cc'=>'finance.php?do=In.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'收入信息' => array('cc'=>'finance.php?do=In.group', 'js'=>''),
			'收入明细' => array('cc'=>'finance.php?do=InItem.group', 'js'=>''),
			
			'银行到帐' => array('cc'=>'finance.php?do=Transaction.group', 'js'=>''),
			'提成信息' => array('cc'=>'finance.php?do=Commission.group', 'js'=>''),
		
			'支出信息' => array('cc'=>'finance.php?do=Out.group', 'js'=>''),
			'支出明细' => array('cc'=>'finance.php?do=OutItem.group', 'js'=>''),	
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));
		
		$controller->assign('pagesize_s', '500');
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '业务员',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);
				
		$controller->display($request, 'finance/in/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name like '%" . $name_s . "%' OR content like '%" . $name_s . "%')";
		}
		
		$where .= $this->_where_x("type", $request['type_s']);
				
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'161', '2'=>'162', '3'=>'173');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}		

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				// $row['num_'] = $this->_num($row['id']);
				$row['abc'] = $this->_in_item2($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/in/list');
		return true;
	}
	
	function _in_item2($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
	
		$data = $aa->_list($id);
		$ret = array("a" => 0, "n" => 0);
		while (list($k, $v) = each($data)) {
			$ret['a'] += $v['total'];
			$ret['n']++;
		}
	
		return $ret;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		// $fmt = "SELECT * FROM %s a %s %s LIMIT 20";
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			$row['num_'] = $this->_num($row['id']);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'finance/in/list');
		return true;
	}
	
	function _num($id) {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
		
		return $aa->_count($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));
		
		// $row['item'] = json_encode($this->_item_blank());		
		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'finance/in/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
						
		$customer = $this->_input($request['customer']);
		$customer_x = addslashes($request['customer_x']);
		
		$mold = $this->_input($request['mold']);
		$project = $this->_project_m($mold);
		
		$name = addslashes($request['name']);
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$date = $request['date'];		
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
				
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, customer, customer_x, project, mold, name, type, currency, amount, rate, rmb, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $customer, $customer_x, $project, $mold, $name, $type, $currency, $amount, $rate, $rmb, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		$request['id_s'] = $id;
		$request['id'] = $id;
		
		$this->_insert_in_item($request);	
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_in_item($request) {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
		
		return $aa->_insert($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
			
		$row['item'] = json_encode($this->_in_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/in/edit');
		return true;
	}
	
	function _in_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
		
		return $aa->_list($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$customer = $this->_input($request['customer']);
		$customer_x = addslashes($request['customer_x']);

		$mold = $this->_input($request['mold']);
		$project = $this->_project_m($mold);
		
		$name = addslashes($request['name']);
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$date = $request['date'];	
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$this->_update_in_item($request);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', customer = '%s', customer_x = '%s', project = '%s', mold = '%s', name = '%s', type = '%s', ";
		$fmt .= "currency = '%s', amount = '%s', rate = '%s', rmb = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $customer, $customer_x, $project, $mold, $name, $type, $currency, $amount, $rate, $rmb, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_in_item($request) {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
		
		$aa->_update($request);
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);

		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/in/upload');
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
			
		$row['item'] = json_encode($this->_in_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/in/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['item'] = $this->_in_item($row['id']);
		// debug(print_r($row, true));
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/in/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		
		$row['exchange'] = $this->_exchange($row['currency']);
		
		$row['customer_t'] = Name::customer_code($row['customer']);
		$row['customer_x'] = json_decode($row['customer_x']);
		
		if ($row['currency'] == 'USD') {
			$row['usd'] = $row['amount'];
		} else {
			$row['usd'] = $row['rmb'] / $this->_exchange('USD');
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('149') || $this->_audit('150')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _delete_in_item($id) {
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
		$aa->_delete_in($id);
	}
	
	function move_id($customer, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE customer = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $customer);
		debug($sql);
		$db->query($sql);
		
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$in = new InItem();
		$in->move_id($customer, $to);
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
		
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$in = new InItem();
		$in->move($from, $to);
	}
	
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$key = sprintf("%s_code_%s", $this->tbl, $q);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$row['status_t'] = $this->status[$row['status']];

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}
	
	function batch_begin($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
	
		$controller->display($request, 'finance/in/batch_begin');
		return true;
	}
	
	function batch_beginPost($controller, $request) {
		$this->pretreat($controller);
	
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
			debug($sql);
			$db->query($sql);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-3 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按本币金额',
			'3' => '按人民币',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/in/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a ";
		} else if ($num_s == '3') {
			$fmt = "SELECT %s AS a, SUM(rmb) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(amount) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'type') {
			$row['a'] = $this->type[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function z($controller, $request) {
		
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE amount = 0";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$amount = $aa->_list_amount($row['id']);			
			
			$fmt = "UPDATE %s SET amount = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $amount, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function z_status($controller, $request) {
	
		require_once(dirname(__FILE__) . '/InItem.class.php');
		$aa = new InItem();
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status < 3";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$status = $aa->_list_status($row['id']);
				
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $status, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function x($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ORDER BY id DESC";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$customer_x = $this->_x($row['customer'], $row['customer_x']);
			
			$fmt = "UPDATE %s SET customer_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $customer_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _x($id, $supplier_x) {
		$j = json_decode($supplier_x);
		
		// var_dump($j);
		
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$aa = new Customer();
		
		$c = $aa->_view($id);
				
		$ret['name'] = $c['name'];
		$ret['fullname'] = $c['fullname'];
		$ret['address'] = $c['address'];
		$ret['web'] = $c['url'];
		
		if (empty($j->contact)) {
			$ret['contact'] = $j->name;
		} else {
			$ret['contact'] = $j->contact;
		}
		
		$ret['mobile'] = $j->mobile;
		$ret['tel'] = $j->tel;
		$ret['fax'] = $j->fax;
		$ret['email'] = $j->email;		
		$ret['content'] = $j->content;
		
		$ret = json_encode($ret);
		
		return addslashes($ret);
	}
	
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '收入信息（已下单）';

		$this->result_s = 20;
		$this->groupby_s = 'currency';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj_a($controller, $request) {	
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, SUM(amount) as c FROM %s a WHERE date >= %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function _update_status_3($id) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '3' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _u2($id, $status) {		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == $status) {
				return false;
			}
		}
		
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// echo $sql . "\n";
		$db->query($sql);
		return true;
	}
	
	function view_customer($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_customer($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/in/_list_c');
		return true;
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/in/_list_c');
		return true;
	}
	
	function t($controller, $request) {

		$db = newdb($this->dsn_w);
		// $fmt = "SELECT * FROM %s WHERE mold is NULL";
		$fmt = "SELECT * FROM %s WHERE mold is NULL OR mold = 0";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			
			$mold = $this->_t_mold($row['name']);
			if (empty($mold)) {
				echo "S";
				continue;
			}
			
			$fmt = "UPDATE %s a, mold b SET a.mold = b.id WHERE b.code = '%s' AND a.id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $mold, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		echo "\n" . $_REQUEST['do'] . "\n";
	
		logdate($controller, $request);
		return true;
	}
	
	function _t_mold($s) {
		echo $s . "\n";
		
		if (preg_match("/^(TM|T|GM)(\d+)\.(\d+)-(\d+)/", $s, $m)) {
			print_r($m);
			
			return $m[0];
		}
		
		if (preg_match("/^(TM|T|GM)(\d+)\.(\d+)/", $s, $m)) {
			print_r($m);
			
			return $m[0];
		}
		
		return '';
	}
}
?>